package leesoft.gocrm.servlet.api;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import net.sf.json.JSONArray;

import leesoft.gocrm.models.Contact;
import leesoft.gocrm.models.dummy.DummySearchContact;
import leesoft.gocrm.models.interfaces.ISearchContact;
import leesoft.gocrm.utils.APIError;

public class ContactServlet extends HttpServlet {
	 /**
     * Handles GET - calls doRequest()
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        doRequest(request, response);
    }


    /**
     * Handle a POST request - calls doRequest()
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        doRequest(request, response);
    }
    
    private void doRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    	//sevlet?by=location&location=someplace
    	String method=request.getParameter("by");
    	String param=request.getParameter(method);
    	if(method!=null&&method.equals("location"))
    	{
    		ISearchContact isearch=new DummySearchContact();
    		List<Contact> result=isearch.searchContactByKeyword(param);
    		
    		JSONArray array= JSONArray.fromObject(result);
    		response.getWriter().print(array);
    	}else
    	{
    		response.getWriter().print(APIError.UNSUPPORT_METHOD);
    	}
    }
    
}
